<?php
class Mozg_Form_Entity
{
	protected $_config;
	protected $_editableFields = array();
	protected $_groupEditableFields = array();
	protected $_obj;
	protected $_id;

	public function __construct(Mozg_Form_Config $config, Mozg_Property_Params $obj)
	{
		$this->_config = $config->GetConfig();
		$this->_obj = $obj;
		$this->_id = $obj->Unique();

		$this->ParseConfig();
	}

	public function GetTitle()
	{
		return isset($this->_config['title']) ? $this->_config['title'] : '';
	}

	public function GetVar()
	{
		if (!isset($this->_config['var']))
		{
			$this->_config['var'] = 'data';
		}

		return $this->_config['var'];
	}

	public function GetGroups()
	{
		return array_keys($this->_config['groups']);
	}

	public function GetGroupConfig($groupName)
	{
		if (!isset($this->_config['groups'][$groupName]))
		{
			$this->_config['groups'][$groupName] = array();
		}

		return $this->_config['groups'][$groupName];
	}

	public function GetGroupTitle($groupName)
	{
		$config = $this->GetGroupConfig($groupName);
		return isset($config['title']) ? $config['title'] : '';
	}

	public function GetGroupParams($groupName)
	{
		$config = $this->GetGroupConfig($groupName);
		return isset($config['params']) ? $config['params'] : array();
	}

	public function GetGroupFields($groupName)
	{
		$config = $this->GetGroupConfig($groupName);

		if (!isset($config['fields'])) return array();
		return array_keys($config['fields']);
	}

	public function GetFieldConfig($groupName, $fieldName)
	{
		$config = $this->GetGroupConfig($groupName);
		return isset($config['fields'][$fieldName]) ? $config['fields'][$fieldName] : array();
	}

	public function GetGroupEditableFields($groupName)
	{
		return isset($this->_groupEditableFields[$groupName]) ? $this->_groupEditableFields[$groupName] : array();
	}

	public function GetEditableFields()
	{
		return $this->_editableFields;
	}

    public function GetUniqueId($groupName, $fieldName, $lang = false)
    {
    	return $this->_id. '_' .$this->GetVar(). '_' .$groupName. '_' .$fieldName.($lang ? '_' .$lang : '');
    }

    public function GetFullName($groupName, $fieldName, $lang = false)
    {
    	return $this->GetVar(). '[' .$groupName. ']' . '[' .$fieldName. ']' .($lang ? '[' .$lang. ']' : '');
    }

	protected function ParseConfig()
	{
		if (!is_array($this->_config['groups']))
		{
			$this->_config['groups'] = array();
		}

		foreach ($this->_config['groups'] as $groupName => $groupData)
		{
			if (!isset($groupData['fields'])) continue;

			foreach ($groupData['fields'] as $fieldName => $fieldParams)
			{
				$showMethod = 'Show' .$fieldName;
				if (method_exists($this, $showMethod) && !$this->$showMethod($groupName))
				{
					unset($this->_config['groups'][$groupName]['fields'][$fieldName]);
					continue;
				}

				if (!isset($fieldParams['editable']) || $fieldParams['editable'])
				{
					$editableMethod = 'Editable' .$fieldName;
					if (method_exists($this, $editableMethod))
					{
						$fieldParams['editable'] = (bool)$this->$editableMethod($groupName);
					}
					else if (!isset($fieldParams['editable']))
					{
						$fieldParams['editable'] = true;
					}
				}

				if ($fieldParams['editable'])
				{
					$this->_groupEditableFields[$groupName][] = $fieldName;
					$this->_editableFields[$fieldName] = true;
				}

				if (isset($fieldParams['values']) && !is_array($fieldParams['values']))
		    	{
		    		$fieldParams['values'] = Mozg_Values::Get($fieldParams['values']);
		    	}

				if ($fieldParams['source'])
				{
					list($source, $sourceLang) = explode('::', $fieldParams['source']);
					$fieldParams['source'] = $this->GetUniqueId($groupName, $source, $sourceLang);
				}

		    	$this->_config['groups'][$groupName]['fields'][$fieldName] = $fieldParams;
			}
			$this->_editableFields = array_keys($this->_editableFields);
		}
	}
}
